package com.syq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syq.mapper.DtsGoodsAttributeMapper;
import com.syq.mapper.DtsGoodsMapper;
import com.syq.mapper.DtsGoodsProductMapper;
import com.syq.mapper.DtsGoodsSpecificationMapper;
import com.syq.pojo.DtsGoods;
import com.syq.pojo.DtsGoodsAttribute;
import com.syq.pojo.DtsGoodsProduct;
import com.syq.pojo.DtsGoodsSpecification;
import com.syq.service.DtsGoodsService;
import com.syq.util.ResponseUtil;
import com.syq.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: DtsGoodsServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/22 11:12
 */

@Service
@Transactional
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;

    @Autowired
    private DtsGoodsProductMapper dtsGoodsProductMapper;

    @Autowired
    private DtsGoodsAttributeMapper dtsGoodsAttributeMapper;

    @Autowired
    private DtsGoodsSpecificationMapper dtsGoodsSpecificationMapper;

    @Override
    public Long findCount() {
        return dtsGoodsMapper.selectCount(null);
    }

    @Override
    public List<DtsGoods> findAll(int pn, int ps, String sort, String order) {
        Page<DtsGoods> page=new Page<>(pn,ps);
        QueryWrapper<DtsGoods> dtsGoodsQueryWrapper = new QueryWrapper<>();
        dtsGoodsQueryWrapper.orderBy(false,false,sort);
        Page<DtsGoods> dtsGoodsPage = dtsGoodsMapper.selectPage(page, dtsGoodsQueryWrapper);
        return dtsGoodsPage.getRecords();
    }

    @Override
    public DtsGoods findById(Integer id) {
        return dtsGoodsMapper.selectById(id);
    }

    @Override
    public void update(DtsGoods goods, Integer id) {
        dtsGoodsMapper.update(goods,new QueryWrapper<DtsGoods>().eq("id",id));
    }

    @Override
    public Object delete(DtsGoods dtsGoods) {
        Integer gid=dtsGoods.getId();
        if (gid==null){
            return ResponseUtil.badArgument();
        }
        dtsGoodsMapper.deleteById(gid);
        dtsGoodsProductMapper.delete(new QueryWrapper<DtsGoodsProduct>().eq("goods_id",gid));
        dtsGoodsAttributeMapper.delete(new QueryWrapper<DtsGoodsAttribute>().eq("goods_id",gid));
        dtsGoodsSpecificationMapper.delete(new QueryWrapper<DtsGoodsSpecification>().eq("goods_id",gid));
        return ResponseUtil.ok();
    }

    @Override
    public void create(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        dtsGoodsMapper.insert(goods);
        for (DtsGoodsProduct product : products) {

            dtsGoodsProductMapper.insert(product);
        }
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            dtsGoodsAttributeMapper.insert(attribute);
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            dtsGoodsSpecificationMapper.insert(specification);
        }
    }

    @Override
    public List<DtsGoods> findByOrderId(Integer id) {
        return dtsGoodsMapper.selectByOrderId(id);
    }
}
